package com.freshcity.page.tree
{
	import com.freshcity.IPresentWater;
	import com.freshcity.component.container.Component;
	import com.freshcity.core.Dispatcher;
	import com.freshcity.core.FilterConst;
	import com.freshcity.core.GlobalVar;
	import com.freshcity.core.OpCodes;
	import com.freshcity.lib.event.EventName;
	import com.freshcity.lib.tooltip.ToolTipsManager;
	import com.freshcity.tree.GiveWaterWin;
	import com.freshcity.tree.OutWaterWin;
	import com.freshcity.utils.MessageUtil;
	import com.freshcity.utils.StringUtil;
	
	import fl.events.ListEvent;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	public class TreeView extends Component implements IPresentWater
	{
		private var _data:*;
		public var waterBar:WaterBar;

		private var back:OutWaterWin;
		private var logTf:TextField;

		private var style:StyleSheet=new StyleSheet();
		private static const CSS:String="".concat('.my_status{color: #C5C5C5;letter-spacing:4px;}', "a{color: #2595B7;}", ".c_tx4{color: #FE6600;}", "div{display: block;}", "span{}", "");

		public function TreeView(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
		{
			style.setStyle("a:link", {fontFamily: '黑体', color: '#003399', fontSize: '14'});
			style.setStyle("a:hover", {color: '#ff0000', textDecoration: 'underline', fontSize: '14'});

			super(parent, xpos, ypos);
			back.treeItem.treeLabel.addEventListener(TextEvent.LINK, onTreeLabelClick);
			back.jiaoshuiBtn.addEventListener(MouseEvent.CLICK, onJiaoshuiBtnClick);
			back.logPanel.tabMenu.addEventListener(ListEvent.ITEM_CLICK, onRenderTreeLog);
		}

		private function onRenderTreeLog(event:ListEvent):void
		{
			var index:int=event.index;
			switch (index)
			{
				case 1:
				{
					logTf.htmlText=MessageUtil.friendRank(data.friendRank as Array);
					break;
				}

				default:
				{
					logTf.htmlText=MessageUtil.decodeTreeLog(data.treeLog);
					break;
				}
					back.logPanel.panel.update();
			}
		}

		private function onTreeLabelClick(event:TextEvent):void
		{
			main.onAboutUser(event);
		}

		protected function onJiaoshuiBtnClick(event:MouseEvent):void
		{
			var give:GiveWater=new GiveWater();
			give.main=this.main;
			give.tree=this;
			give.show();
		}
		
		public function disableSubmitButton():void
		{
			back.jiaoshuiBtn.labelName.text="工作中...";
			switchBtn(false);
			Dispatcher.addEventListener(EventName.FinishTimeTick,initTree);
		}


		override protected function addChildren():void
		{
			super.addChildren();
			back=new OutWaterWin();
			addChild(back);

			back.errorTip.visible=false;
			back.newFeijiLink.visible=false;

			back.treeItem.treeLabel.styleSheet=style;
			waterBar=new WaterBar(back, 104, 256);

			//初始化成长动态
			logTf=new TextField();
			logTf.x=5;
			logTf.y=20;
			logTf.autoSize=TextFieldAutoSize.LEFT;
			logTf.wordWrap=true;
			logTf.multiline=true;
			logTf.width=180;

			var format:TextFormat=new TextFormat("宋体", 12);
			format.leading=6;
			logTf.defaultTextFormat=format;

			var style:StyleSheet=new StyleSheet();
			style.parseCSS(TreeView.CSS);
			logTf.styleSheet=style;
			back.logPanel.panel.source=logTf;
		}

		public function renderTree(data:*):void
		{
			this.data=data;
			this.initTree();
			super.show();
		}
		
		private function initTree(event:Event=null):void
		{
			Dispatcher.removeEventListener(EventName.FinishTimeTick,initTree);
			
			back.treeItem.gotoAndStop(data.growLevel + 1);
			var maxValue:int=GlobalVar.MAX_TREE_LEVEL * GlobalVar.UPGRADE_LEVEL_VALUE;
			var i:int=(data.growValue / maxValue) * back.growBar.totalFrames;
			back.growBar.gotoAndStop(i);
			back.growBar.toolTipData="成长度 " + data.growValue + "/" + maxValue;
			ToolTipsManager.register(back.growBar);
			
			if (data.owner == main.user.id)
			{
				back.treeItem.treeLabel.htmlText="我的梧桐树";
			}
			else
			{
				back.treeItem.treeLabel.htmlText="".concat("<a href='event:", String(data.owner), "'>", "用户", data.owner + "的梧桐树", "</a>");
			}
			
			var supplyWater:String=data.waterInfo;
			if (main.user.sex == 'female'){
				back.logPanel.tabMenu.arrBtnLabels=["成长动态"];
			}
			back.logPanel.tabMenu.selectedIndex=0;
			logTf.htmlText=MessageUtil.decodeTreeLog(data.treeLog);
			back.logPanel.panel.update();
			
			waterBar.main=this.main;
			renderWaterBar(supplyWater);
			
			if (main.user.sex == 'female')
			{
				back.jiaoshuiBtn.visible=false;
			}
			
			if (data.growValue >= maxValue)
			{
				back.jiaoshuiBtn.labelName.text="已成熟";
				switchBtn(false);
			}
			else if (supplyWater.indexOf("0") <= -1)
			{
				back.jiaoshuiBtn.labelName.text="蓄水充足";
				switchBtn(false);
			}
			else if (main.statusPanel.timeBar.isTick)
			{
				this.disableSubmitButton();
			}
			else
			{
				back.jiaoshuiBtn.labelName.text="送水给TA";
				switchBtn(true);
			}
		}
		
		private function renderWaterBar(waterStr:String):void
		{
			waterBar.setData(waterStr);
			waterBar.show();
		}

		private function switchBtn(flag:Boolean):void
		{
			if (flag)
			{
				back.jiaoshuiBtn.filters=[];
				back.jiaoshuiBtn.mouseEnabled=true;
				back.jiaoshuiBtn.mouseChildren=true;
			}
			else
			{
				back.jiaoshuiBtn.filters=[FilterConst.colorMatrixFilterGray];
				back.jiaoshuiBtn.mouseEnabled=false;
				back.jiaoshuiBtn.mouseChildren=false;
			}
			back.jiaoshuiBtn.labelName.mouseEnabled=false;
		}

		override public function destroy():void
		{
			ToolTipsManager.unregister(back.growBar);
			back.jiaoshuiBtn.removeEventListener(MouseEvent.CLICK, onJiaoshuiBtnClick);
			super.destroy();
		}

		public function get data():*
		{
			return _data;
		}

		public function set data(value:*):void
		{
			_data = value;
		}


	}
}